 //获取焦点变色
 var regFoucs = function(ele){
	 //边框变色
	 $(ele).css({"border":"1px solid","border-color":"#ff9900"});
 }
// 失去焦点结果为ture时的变色
var regRight = function(ele){
	//输入框颜色变为绿色
	$(ele).css({"border":"1px solid","border-color":"#59A358"});
	//输入框字体变为黑色
	$(ele).css("color","#000000");
}
//失去焦点结果为false时变色
var regWorng = function(ele){
	//输入框颜色变为红色
	$(ele).css({"border":"1px solid","border-color":"#e60000"});
	//输入框字体变为红色
	$(ele).css("color","#e60000");
}
//当手机号码输入框获取焦点
$("#phone-ipt").eq(0).focus(function(){
	regFoucs($(this));
})
var phone = true;
//手机号码输入框失去焦点时;
$("#phone-ipt").blur(function(){
	//如果手机号不正确或输入框为空,返回false
	if(!(/^1[34578]\d{9}$/.test($("#phone-ipt").val())) || $("#phone-ipt").val() == null){ 
		//错误图标出现
        $("#Error-1").css("display","block");
		regWorng($(this));
		phone = false;
	}else{
		//错误提示框隐藏
		$("#Error-1").css("display","none");
		regRight($(this));
	}
 });
 var password = true;
 // 输入密码获取焦点
 $("#password-ipt").eq(0).focus(function(){
	 regFoucs($(this));
 })
 //声明变量regPassword的构造函数
 var regPassword = function(passWord){
 	//将正则的三个验证保存到regArr数组中
 	var regArr = [
 		{reg:/^(?:\d+|[a-zA-Z]+|[!@#$%^&*]+){6,20}$/},
 		{reg:/^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*]+$/},
 		{reg:/^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)(?![a-zA-z\d]+$)(?![a-zA-z!@#$%^&*]+$)(?![\d!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*]+$/}
 		];
 	//假设passFalse为false
 	var passFalse = false;
 	// 循环遍历regArr数组中的元素 
 	for(var i = 0 ; i < regArr.length;i++){
 		//如果获取焦点的元素存在
 		if(regArr[i].reg.test(passWord)){
 			//让passFalse为true
 			passFalse = true;
 			//停止循环
 			break;
 		}
 	}
 	//如果passFalse为true 执行下面的代码
 	if(passFalse){
 		//报错提示框隐藏
 		$("#Error-2").css("display","none");
 		regRight($("#password-ipt"));
 	}else{
 		$("#Error-2").css("display","block");
 		regWorng($("#password-ipt"));
 		password = false;
 	}
 }
 
  //输入密码失去焦点;
  $("#password-ipt").blur(function(){
 	 //调用构造函数
 	 regPassword($(this).val());
  })
  
  //提交按钮点击事件
  $("#button_ipt").on("click",function(){
  	if(phone == false || password == false){
  		$("#Error-1").css("display","block");
  		$("#Error-2").css("display","block");
  	}else if($("#phone-ipt").val().length == 0 || $("#password-ipt").length == 0){
  		$("#Error-1").css("display","block");
  		$("#Error-2").css("display","block");
  	}else{
  		var url = "http://jx.xuzhixiang.top/ap/api/login.php";
  		$.ajax({
  			url:url,
  			type:"get",
  			data:{"username":$("#phone-ipt").val(),"password":$("#password-ipt").val()},
  			success:function(res){
  				var obj = JSON.parse(res);
  				if(obj.code){
  					alert("登录成功");
						setCookie("token",obj.data.id,7,"/");
  					$(location).attr('href', 'index.html');
  				}else{
					alert("登录失败");
  				}
  			}
  		})
  		
  		
  	}
  })